<?php
/** Ajax 
	* 
	* @package Pharmacy Intervention 
	* @subpackage Controller
	* @category Ajax 
	* @author Ray Nowell 
	* 
	* This controller is used for gneneral ajax calls
	*/  
class Ajax extends MY_Controller {
  
	function Ajax()
	{
		parent::MY_Controller();
	}
  
	function index()
	{
		echo 'index for Ajax not yet created.  Purpose is only for ajax at the moment';
	}
  
	/** 
	* Returns the last modified value of the record in question
	* 
	* @access public 
	* @return Text that is the last modified date */ 
	function record_modified_date()
	{
		$this->load->model('Generic_model');
		
		$controller_name	=	$this->uri->segment(3);
		$record_id			=	$this->uri->segment(4);
		switch ($controller_name) 
		{
			case 'intervention':
				$table_name =	'clinical_intervention';
				$id_field	=	$controller_name.'_id';
				break;
			default:
				$table_name	=	$controller_name;
				$id_field	=	$controller_name.'_id';
		}
	
		$modified_date = $this->Generic_model->get_modified_date($table_name,$id_field,$record_id);
		echo  $modified_date;
	}

	function copy_intervention()
	{
		$this->load->model('Intervention_model');
		
		$copy_date	=	null;
		$ret_val = 'Nothing';
		
		//Left off here
		$copy_to_next_month	=	$this->uri->segment(4)	===	'true';
		
		if (!$copy_to_next_month)
		{
			$date_var = explode('_',$this->uri->segment(5));
			$copy_date = $date_var[2].'-'.$date_var[0].'-'.$date_var[1];
		}
		
		//Get Copy Ids
		$intervention_id_list	=	'';
		$total_segments	=	$this->uri->total_segments();
		for ($i = 6; $i <= $total_segments; $i++) 
			$intervention_id_list .= $this->uri->segment($i).',';
		
		$intervention_id_list = substr($intervention_id_list,0,-1); 
		
		$result	=	$this->Intervention_model->copy_intervention($copy_to_next_month,$copy_date,$intervention_id_list);
		
		if ($result)
			$ret_val	=	'Copy successful.';
		else
			$ret_val	=	'Copy did not work.';
		
		echo  $ret_val;
	}
	
	/** 
	* Returns a list of drugs found based on the criteria posted.
	* Designed for use as an ajax call on the Clinical Intervention Form.
	* If an Ajax call is detected the data found is encoded and echoed as JSON data.
	* 
	* @access public 
	* @return nothing */ 
	function drug_list_lookup()
		{
			//$this->output->enable_profiler(TRUE);
			$this->load->model('Drug_model');
			// process posted form data (the requested product)
			$keyword = $this->input->post('term');
			$data['response'] = 'false'; //Set default response
			$query = $this->Drug_model->seek_drugs($keyword); //Search DB
			if( ! empty($query) )
			{
				$data['response'] = 'true'; //Set response
				$data['message'] = array(); //Create array
				foreach( $query as $row )
				{
					$data['message'][] = array(   'id'  => $row->drug_id,
																				'value'=> $row->full_drug_description,
																				'wholesale_each_cost'=> $row->wholesale_each_cost,
																				'doses_per_day'=> $row->doses_per_day
																				); //Add a row to array
					}
				}
				if(IS_AJAX)
				{
					echo json_encode($data); //echo json string if ajax request
				}
				else
				{
					$this->load->view('drug_list/index',$data); //Load html view of search results
				}
			} // end of lookup  
	
}  
/* End of file ajax.php */
/* Location: ./system/application/controllers/ajax.php */
